﻿/*/
 * Fuzzy Number Calculator v 1.0
 * By Ahmed Ezz.
 *  Student at Final year Computer & system Engineering
 *  Al-Azhar University
 *  -- last modification at: 27/12/2008
/*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FuzzyCalc
{
    public partial class frmFuzzySetCalc : Form
    {
        public frmFuzzySetCalc()
        {
            InitializeComponent();
        }

        DataTable tableOfSets;
        int SetCardinality;

        private void btnSetCardinality_Click(object sender, EventArgs e)
        {   
            // init new table            
            tableOfSets = new DataTable("SetsTable");

            // get set cardinality
            int sc = Convert.ToInt32(txtSetCardinality.Text);
            SetCardinality = sc;

            // add columns
            for (int i = 0; i < sc; i++)
            {
                tableOfSets.Columns.Add("X" + i.ToString());
                tableOfSets.Columns[i].DefaultValue = 0;                
            }

            // add 3 rows (one for Set A, one for Set B and the third for result)
            DataRow row = tableOfSets.NewRow();
            tableOfSets.Rows.Add(row);
            row = tableOfSets.NewRow();
            tableOfSets.Rows.Add(row);
            row = tableOfSets.NewRow();
            tableOfSets.Rows.Add(row);

            // show the new designed table
            gridSets.DataSource = tableOfSets;
            gridSets.RowHeadersWidth = 60;
            gridSets.Refresh();            
        }

        private void gridSets_CellPainting(object sender, 
            DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex < 0)
            {
                // we are about to draw header cells

                e.PaintBackground(e.ClipBounds, false);
                e.PaintContent(e.ClipBounds);

                if (e.RowIndex < 0)
                    e.Graphics.DrawString("Sets", Font, Brushes.Black,
                        new PointF(e.CellBounds.X + 15, e.CellBounds.Y + 5));

                // first row
                if (e.RowIndex == 0)
                    e.Graphics.DrawString("Set A", Font, Brushes.Black,
                        new PointF(e.CellBounds.X + 15, e.CellBounds.Y + 5));

                // second row
                if (e.RowIndex == 1)
                    e.Graphics.DrawString("Set B", Font, Brushes.Black,
                        new PointF(e.CellBounds.X + 15, e.CellBounds.Y + 5));

                // second row
                if (e.RowIndex == 2)
                    e.Graphics.DrawString("Result", Font, Brushes.Black,
                        new PointF(e.CellBounds.X + 15, e.CellBounds.Y + 5));

                e.Handled = true;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnUnion_Click(object sender, EventArgs e)
        {
            // get the maximum
            for (int i = 0; i < SetCardinality; i++)
            {
                // set the value of result(i) = max of the i(th) between A and B
                tableOfSets.Rows[2][i] = 
                    Math.Max(Convert.ToSingle(tableOfSets.Rows[0][i].ToString()),
                             Convert.ToSingle(tableOfSets.Rows[1][i].ToString()));
            }
        }

        private void btnIntersection_Click(object sender, EventArgs e)
        {
            // get the minimum
            for (int i = 0; i < SetCardinality; i++)
            {
                // set the value of result(i) = min of the i(th) between A and B
                tableOfSets.Rows[2][i] =
                    Math.Min(Convert.ToSingle(tableOfSets.Rows[0][i].ToString()),
                             Convert.ToSingle(tableOfSets.Rows[1][i].ToString()));
            }
        }

        private void gridSets_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            // get the value of the cell that has been edited
            string s = gridSets.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            float f = Convert.ToSingle(s);
            // the data entered must be in range 0 : 1
            if (!(f >= 0 && f <= 1))
            {
                // if not, so we revert it back to default 0 value
                gridSets.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "0";
            }
        }

        private void btnCompA_Click(object sender, EventArgs e)
        {
            // complement each value (new value = 1 - old value)
            for (int i = 0; i < SetCardinality; i++)
            {
                // set the value of result(i) = 1 - i(th) of A
                tableOfSets.Rows[2][i] = 
                    (1)-(Convert.ToSingle(tableOfSets.Rows[0][i].ToString()));
            }
        }

        private void btnCompB_Click(object sender, EventArgs e)
        {
            // complement each value (new value = 1 - old value)
            for (int i = 0; i < SetCardinality; i++)
            {
                // set the value of result(i) = 1 - i(th) of B
                tableOfSets.Rows[2][i] =
                    (1) - (Convert.ToSingle(tableOfSets.Rows[1][i].ToString()));
            }
        }

        private void btnAimplyB_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < SetCardinality; i++)
            {
                float a = Convert.ToSingle(tableOfSets.Rows[0][i].ToString());
                float b = Convert.ToSingle(tableOfSets.Rows[1][i].ToString());

                if (a >= b)
                    tableOfSets.Rows[2][i] = "YES";
                else
                    tableOfSets.Rows[2][i] = "NO";
            }
        }

        private void btnBimplyA_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < SetCardinality; i++)
            {
                float a = Convert.ToSingle(tableOfSets.Rows[0][i].ToString());
                float b = Convert.ToSingle(tableOfSets.Rows[1][i].ToString());

                if (b >= a)
                    tableOfSets.Rows[2][i] = "YES";
                else
                    tableOfSets.Rows[2][i] = "NO";
            }
        }

        private void btnDifference_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < SetCardinality; i++)
            {
                float a = Convert.ToSingle(tableOfSets.Rows[0][i].ToString());
                float b = Convert.ToSingle(tableOfSets.Rows[1][i].ToString());

                if (b >= a)
                    tableOfSets.Rows[2][i] = "0";
                else
                    tableOfSets.Rows[2][i] = (a - b).ToString();
            }
        }
    }
}
